package com.example.schooltest.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.schooltest.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    private ListView listView;

    private List<Map<String,Object>> list;
    private String[] name = {"剑豪", "艾克", "狗头","狂暴之心","武器大师","豹女","暗裔剑魔","雪人骑士","卡特琳娜","亚索",
            "剑豪", "艾克", "狗头","狂暴之心","武器大师","豹女","暗裔剑魔","雪人骑士","卡特琳娜","亚索"};
    private String[] talk = {"吾之荣耀，离别已久", "我宁愿犯错误，也不愿什么都不做",
            "你的遗体将慢慢消散，化为永恒，就像沙漠中的沙砾那样",
            "我对你的爱就如闪电，狂暴而剧烈，飞出的飞镖，是我向你传达的思念",
            "哼，一个能打的都没有", "狂野将使他们感到畏惧",
            "这场战争，将会是我的杰作", "不管是怎么样的天气，不管是什么样的环境，不管有什么样的危险，我的伙伴，雪人，一直都会陪着我",
            "永远不要质疑我的忠诚，你不会了解我为之忍受的一切",
            "我不会怀着耻辱而死",
            "吾之荣耀，离别已久", "我宁愿犯错误，也不愿什么都不做",
            "你的遗体将慢慢消散，化为永恒，就像沙漠中的沙砾那样",
            "我对你的爱就如闪电，狂暴而剧烈，飞出的飞镖，是我向你传达的思念",
            "哼，一个能打的都没有", "狂野将使他们感到畏惧",
            "这场战争，将会是我的杰作", "不管是怎么样的天气，不管是什么样的环境，不管有什么样的危险，我的伙伴，雪人，一直都会陪着我",
            "永远不要质疑我的忠诚，你不会了解我为之忍受的一切",
            "我不会怀着耻辱而死"
    };
    private int[] img = {R.drawable.img, R.drawable.img_1, R.drawable.img_2,
            R.drawable.img_3, R.drawable.img_4, R.drawable.img_5,
            R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9,
            R.drawable.img, R.drawable.img_1, R.drawable.img_2,
            R.drawable.img_3, R.drawable.img_4, R.drawable.img_5,
            R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9
    };
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView)view.findViewById(R.id.lv);

        list = new ArrayList<Map<String, Object>>();

        for(int i=0;i<name.length;i++){
            Map<String, Object> map = new HashMap<>();
            map=new HashMap<String,Object>();
            map.put("img", img[i]);
            map.put("name",name[i]);
            map.put("talk",talk[i]);
            list.add(map);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(view.getContext(), list, R.layout.actvity_list_item,
                new String[]{"img","name", "talk"}, new int[]{R.id.img,R.id.tv_name, R.id.tv_talk});

        listView.setAdapter(simpleAdapter);
    }
}